package File;

import java.io.File;
import java.util.Scanner;

public class Homework1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的根目录:");
        String rootDirPath = scanner.next();
        File rootDir = new File(rootDirPath);
        if(!rootDir.isDirectory()){
            System.out.println("你输入的不是根目录");
            return;
        }

        System.out.println("请输入你要删除的指定字符:");
        String keyword = scanner.next();

        scanDir(rootDir,keyword);

    }

    private static void scanDir(File root, String keyword) {
        File[] files = root.listFiles();
        if(files == null || files.length == 0){
            return;
        }
        for(File file : files){
            if(file.isFile()){
                // 如果是普通文件,判断文件是否包含关键字
                dealFile(file,keyword);
            } else{
                // 如果是目录,就递归调用本方法
                scanDir(file,keyword);
            }

        }
    }

    private static void dealFile(File file, String keyword) {
        if(file.getName().contains(keyword)){
            System.out.println(file.getAbsolutePath() + " 请问你是否要删除这个文件(y/n):");
            Scanner scanner = new Scanner(System.in);
            String response = scanner.next();
            response.equals("y");
            file.delete();
        }
    }
}
